package 方法句柄;

//import static org.junit.jupiter.api.Assertions.*;

import java.lang.invoke.MethodHandle;
import java.lang.invoke.MethodHandles;
import java.lang.invoke.MethodType;
import java.util.Arrays;

import org.junit.jupiter.api.Test;

//import org.junit.jupiter.api.Test;

public class Varargs1 {
	public void normalMethod(String arg1, int arg2, int[] arg3) {
		System.out.println(Arrays.toString(arg3)); // args
	}

	@Test
	public void asVarargsCollector() throws Throwable {
		MethodHandles.Lookup lookup = MethodHandles.lookup();
		MethodType mt=MethodType.methodType(void.class, String.class, int.class, int[].class);
		MethodHandle mh = lookup.findVirtual(Varargs1.class, "normalMethod",mt);
        //简便的写法：
		mh = mh.asVarargsCollector(int[].class);
		mh.invoke(this, "Hello", 2, 1, 4, 5, 7, 8);
		//麻烦的写法
		int[] ns= {1, 4, 5, 7, 8};
		mh.invoke(this, "Hello", 2,ns);
		
		mh = mh.asCollector(int[].class,2);//只能传入2个
		mh.invoke(this, "Hello", 2, 1, 4);

	}
}
